package logic

import (
	"context"
	"errors"
	"video-joy/models/mysql"

	"video-joy/userrpc/internal/svc"
	"video-joy/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectionDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectionDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectionDelLogic {
	return &CollectionDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 取消收藏
func (l *CollectionDelLogic) CollectionDel(in *userrpc.CollectionDelRequest) (*userrpc.CollectionDelResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.UserInfo

	if err := user.UserFindById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("该用户不存在")
	}

	var video mysql.VideoInfo

	if err := video.VideoFindById(mysql.DB, in.VideoId); err != nil {
		return nil, errors.New("该视频不存在")
	}

	var collection mysql.CollectionInfo

	if err := collection.FindCollectionById(mysql.DB, in.CollectionId); err != nil {
		return nil, errors.New("该收藏不存在")
	}

	if err := collection.CollectionDel(mysql.DB); err != nil {
		return nil, errors.New("取消收藏失败")
	}

	return &userrpc.CollectionDelResponse{}, nil
}
